<?php
class TabsController extends AppController {

	var $name = 'Tabs';
	var $uses = array('Tab', 'Link');

	function index($cur_tab = 1) {
		$this->Tab->recursive = 1;
		$tab = $this->Tab->find('first', array(
				'conditions' => array(
					'Tab.user_id' => $this->Auth->user('id'),
					'Tab.position' => $cur_tab
				)
		));
		$list_tabs = $this->Tab->find('list', array(
				'fields' => array('Tab.position', 'Tab.label'),
				'conditions' => array('Tab.user_id' => $this->Auth->user('id')),
				'order' => array('Tab.position')
		));
		$links = $this->Link->find('all', array(
				'conditions' => array('tab_id' => $tab['Tab']['id']),
				'order' => 'Link.created DESC')
		);
		$this->set('list_tabs', $list_tabs);
		$this->set('cur_tab', $cur_tab);
		$this->set('links', $links);
	}

	private function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid tab', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('tab', $this->Tab->read(null, $id));
	}

	function save() {
		if (!empty($this->data)) {
			if (isset($this->data['Tab']['id'])) {
				$tab = $this->Tab->find('first', array(
						'conditions' => array(
								'Tab.position' => $this->data['Tab']['id'],
								'Tab.user_id' => $this->Auth->user('id')
						)
				));
				$this->data['Tab']['id'] = $tab['Tab']['id'];
				unset($this->data['Tab']['position']);
				if ($this->Tab->save($this->data)) {
					$this->redirect(array('controller' => 'tabs', 'action' => 'index', $tab['Tab']['position']));
				}
			} elseif(!isset($this->data['Tab']['id'])) {
				$tab = $this->Tab->find('first', array(
						'conditions' => array(
							'Tab.user_id' => $this->Auth->user('id')
						),
						'order' => array(
							'Tab.position DESC'
						)
				));

				$this->data['Tab']['user_id'] = $this->Auth->user('id');
				$this->data['Tab']['position'] = $tab['Tab']['position'] + 1;

				$this->Tab->create();
				if ($this->Tab->save($this->data)) {
					$this->redirect(array('controller' => 'tabs', 'action' => 'index', $tab['Tab']['position'] + 1));
				}
			}
		}
		$this->redirect('/');
	}

	private function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid tab', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Tab->save($this->data)) {
				$this->Session->setFlash(__('The tab has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The tab could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Tab->read(null, $id);
		}
		$users = $this->Tab->User->find('list');
		$this->set(compact('users'));
	}

	function delete($id = null) {
		$count_tabs = $this->Tab->find('count', array('conditions' => array(
				'Tab.user_id' => $this->Auth->user('id')
		)));
		if ($id && $count_tabs > 1) {
			$this->Tab->deleteAll(array(
					'Tab.user_id' => $this->Auth->user('id'),
					'Tab.position' => $id
			));
			$this->Tab->query("UPDATE tabs SET position = position - 1 WHERE position > $id;");
			$this->redirect(array('controller' => 'tabs', 'action' => 'index', ($id < $count_tabs) ? $id : $id - 1));
		}
		$this->redirect('/');
	}
	function admin_index() {
		$this->Tab->recursive = 0;
		$this->set('tabs', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid tab', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('tab', $this->Tab->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Tab->create();
			if ($this->Tab->save($this->data)) {
				$this->Session->setFlash(__('The tab has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The tab could not be saved. Please, try again.', true));
			}
		}
		$users = $this->Tab->User->find('list');
		$this->set(compact('users'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid tab', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Tab->save($this->data)) {
				$this->Session->setFlash(__('The tab has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The tab could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Tab->read(null, $id);
		}
		$users = $this->Tab->User->find('list');
		$this->set(compact('users'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for tab', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Tab->delete($id)) {
			$this->Session->setFlash(__('Tab deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Tab was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>